﻿using System;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class LicenseDataTests
    {
        private LicensingStrategyFactory licensingStrategyFactory;
        private UncountedFloatingLicense license;
        private CustomerLicenseEntitlement cple;
        private FeatureIdentificationKey fik;
        private ILicensingStrategy licensingStrategy;

        [TestInitialize]
        public void Setup()
        {
            var feature = new Feature();
            license = new UncountedFloatingLicense{Feature = feature};
            cple = new CustomerLicenseEntitlement(Guid.Empty, string.Empty, new LicenseEntitlement{License = license, Product = new Product()}, null);
            fik = cple.ToFeatureIdentificationKey();
            licensingStrategy = Substitute.For<ILicensingStrategy>();
            licensingStrategyFactory = Substitute.For<LicensingStrategyFactory>();
            
            licensingStrategyFactory.GetLicensingStrategyForLicense(license, Arg.Any<LicenseEntitlementValidity>(), fik).Returns(licensingStrategy);
        }

        [TestMethod]
        public void Should_checkout_license_by_calling_licensing_strategy()
        {
            var licenseData = new LicenseData(cple, licensingStrategyFactory);
            licenseData.Checkout(new LicenseRequestInformation {FeatureIdentificationKey = fik});

            licensingStrategy.ReceivedWithAnyArgs().Checkout(null, null);
        }

        [TestMethod]
        public void Should_checkin_license_by_calling_licensing_strategy()
        {
            var licenseData = new LicenseData(cple, licensingStrategyFactory);
            licenseData.Checkin(new LicenseRequestInformation());

            licensingStrategy.ReceivedWithAnyArgs().Checkin(null, null);
        }

        [TestMethod]
        public void Should_reassert_license_by_calling_licensing_strategy()
        {
            var licenseData = new LicenseData(cple, licensingStrategyFactory);
            licensingStrategy.Reassert(Guid.Empty, null).ReturnsForAnyArgs(true);
            var result = licenseData.Reassert(Guid.Empty, null);

            Assert.IsTrue(result);
            licensingStrategy.ReceivedWithAnyArgs().Reassert(Guid.Empty, null);
        }
    }
}